package com.hj.chartdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hj.chartdemo.R;
import com.hj.chartdemo.bean.chart.ChartTabData;

import java.util.ArrayList;

import androidx.recyclerview.widget.RecyclerView;

public class ChartPieTabAdapter extends RecyclerView.Adapter<ChartPieTabAdapter.Vh>
    implements View.OnClickListener {

  private Context context;
  public ArrayList<ChartTabData> stringList;
  public Integer imgType = 1;

  private OnItemClickListener mItemClickListener;

  public ChartPieTabAdapter(Context context, ArrayList<ChartTabData> stringList) {
    this.context = context;
    this.stringList = stringList;
  }

  public ChartPieTabAdapter(Context context) {
    this.context = context;
  }

  public void setData(ArrayList<ChartTabData> list) {
    this.stringList = list;
    notifyDataSetChanged();
  }

  public void refresh() {
    notifyDataSetChanged();
  }

  public void setItemClickListener(OnItemClickListener itemClickListener) {
    mItemClickListener = itemClickListener;
  }

  @Override
  public ChartPieTabAdapter.Vh onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.chart_pie_tab_item, parent,false);
    Vh holder = new Vh(view);
    view.setOnClickListener(this);
    return holder;
  }

  @Override
  public void onBindViewHolder(ChartPieTabAdapter.Vh holder, final int position) {
    ChartTabData data = stringList.get(position);
    holder.tv.setText(data.getLabel());
    holder.iv.setVisibility(data.getSelected() ? View.VISIBLE : View.GONE);
    holder.lv.setBackgroundResource(
        data.getSelected()
            ? R.drawable.rectangle_blue_border_2
            : R.drawable.rectangle_hui_border_2);
    holder.tv.setTextColor(
        data.getSelected()
            ? this.context.getResources().getColor(R.color.blue)
            : this.context.getResources().getColor(R.color.txt_feibitian));
    holder.itemView.setTag(data);
  }

  @Override
  public int getItemCount() {
    return stringList == null ? 0 : stringList.size();
  }

  @Override
  public void onClick(View v) {
    if (mItemClickListener == null) return;
    ChartTabData d = (ChartTabData) v.getTag();
    if (!d.getSelected()) {
      for (int i = 0; i < stringList.size(); i++) {
        if (!stringList.get(i).getKey().equals(d.getKey())) {
          stringList.get(i).setSelected(false);
        }
      }
      d.setSelected(true);
      notifyDataSetChanged();
      mItemClickListener.onItemClick(d);
    }
  }

  public interface OnItemClickListener {
    void onItemClick(ChartTabData data);
  }

  class Vh extends RecyclerView.ViewHolder {
    public Vh(View itemView) {
      super(itemView);
      tv = itemView.findViewById(R.id.chart_pie_title);
      iv = itemView.findViewById(R.id.chart_pie_iv);
      lv = itemView.findViewById(R.id.chart_pie_back);
    }

    private TextView tv;
    private ImageView iv;
    private LinearLayout lv;
  }
}
